package com.example.maple.ccsclient.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.interfaces.ISettingView;
import com.example.maple.ccsclient.presenter.SettingPresenter;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;

public class SettingActivity extends Activity implements ISettingView {

	/*
	* 预先估计，有设置字体预览的列，以及清除已经保存的图片
	*设置字的透明度，可以打印出来练字
	 */

	ProgressBar mBar;
	TextView mTextView;
	Button mClear;
	Button mPic;
	Button mChar;
	Button mizige;
	SettingPresenter mSettingPresenter;

	LinearLayout mCacheView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		mSettingPresenter = new SettingPresenter(this,this);
		findView();
	}

	private void findView() {
		mCacheView = (LinearLayout)findViewById(R.id.cacheView);
		mBar = (ProgressBar)findViewById(R.id.pro);
		mTextView = (TextView)findViewById(R.id.msg);
		mChar = (Button)findViewById(R.id.btn_char);
		mClear = (Button)findViewById(R.id.btn_clear);
		mPic = (Button)findViewById(R.id.btn_pic);
		mizige = (Button)findViewById(R.id.btn_mizige);
		if(SharedPreferencesUtils.getIntersected(this)){
			mizige.setText("米字格(状态:开)");
		}else{
			mizige.setText("米字格(状态:关)");
		}
		mizige.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(SharedPreferencesUtils.getIntersected(SettingActivity.this)){
					SharedPreferencesUtils.setIntersected(SettingActivity.this,false);
					mizige.setText("米字格(状态:关)");
				}else{
					SharedPreferencesUtils.setIntersected(SettingActivity.this,true);
					mizige.setText("米字格(状态:开)");
				}
				Intent i = getBaseContext().getPackageManager()
						.getLaunchIntentForPackage(getBaseContext().getPackageName());
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
			}
		});
		findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mSettingPresenter.cancelTask();
				finish();
			}
		});
		mClear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showCache();
				mSettingPresenter.clearCache();
			}
		});
		hiddenCache();

		mChar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SetColumnActivity.startAction(SettingActivity.this,"文字列设置","设置缺字统计功能，显示的文字列数,当列数为0时，默认为1。" +
						"当完成设置时，点击确定程序自动重启，以便更改设置生效");
			}
		});

		mPic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				SetColumnActivity.startAction(SettingActivity.this,"图片列设置","设置所有图片显示的列数,当列数为0时，默认为1"+
						"当完成设置时，点击确定程序自动重启，以便更改设置生效");
			}
		});
	}

	public static void startAction(Context context) {
		Intent intent = new Intent(context,SettingActivity.class);
		context.startActivity(intent);
	}

	//设置进度条最大值
	@Override
	public void setProgressBarValue(int value) {
		mBar.setMax(value);
	}

	//更新进度条
	@Override
	public void updateProgressBar(int value) {
		mBar.setProgress(value);
	}

	@Override
	public void showDialog(String toastText) {
		Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void showCache() {
		mCacheView.setVisibility(View.VISIBLE);
	}

	@Override
	public void hiddenCache() {
		mCacheView.setVisibility(View.GONE);
	}

	@Override
	public void showClearFile(String info) {
		mTextView.setText(info);
	}
}
